var detailsId = localStorage.getItem('detailsId'),
    token = localStorage.getItem('token'),
    detailsData = '', commentData = '', isLike = '', bodyPart = '', tId = '', cId = ''

if (localStorage.getItem("inDetailsPage") == 0) {
    localStorage.removeItem("inDetailsPage")
    history.go(0)
}

localStorage.setItem("inDetailsPage", 1)

if (localStorage.getItem("Refresh") == 1) {
    localStorage.setItem("Refresh", 0)
    history.go(0)
}

if (!localStorage.getItem("hostVal")) {
    $(".back").on("click", function () {
        window.opener = null;
        window.close();
        WeixinJSBridge.call('closeWindow');
        wx.closeWindow();
    })
}

if (detailsId == null && window.location.href.split("title=")[1] == undefined) {
    stop()
    layer.confirm('请从动态入口进入', {
        btn: ['确定']
        , btn1: function (index, layero) {
            window.location.href = 'homePage.html'
            move()
        }, title: '提示'
    });
} else if (detailsId) {
    detailsData = Sendajax("http://119.23.46.241:8080//home/details/seldetails", "post", {id: parseInt(detailsId)}, token)
    commentData = Sendajax("http://119.23.46.241:8080/home/details/selComment", "post", {id: parseInt(detailsId)}, token)
} else if (window.location.href.split("title=")[1]) {
    detailsId = window.location.href.split("title=")[1]
    detailsData = Sendajax("http://119.23.46.241:8080//home/details/seldetails", "post", {id: parseInt(detailsId)}, token)
    commentData = Sendajax("http://119.23.46.241:8080/home/details/selComment", "post", {id: parseInt(detailsId)}, token)
}


//判断是否登录显示点赞
if (token) {
    isLike = Sendajax("http://119.23.46.241:8080/login/details/islike", "post", {id: parseInt(detailsId)}, token)
    if (isLike.data == 1) {
        $(".like-icon").css("background", "url('https://19jwshow.oss-cn-shenzhen.aliyuncs.com/images/Heart%403x.png') center center no-repeat").attr("data-isLike", 1)
    }
    if (isLike.data == 0) {
        $(".like-icon").attr("data-isLike", 0)
    }
}

//显示发布动态信息
$(".title p").text(`${detailsData.data.title.title}`).attr("data-tId", detailsData.data.title.id)
$(".time").text(`${detailsData.data.title.time}`)
$(".label").text(`${detailsData.data.title.label}`)
$(".username").text(`${detailsData.data.title.username}`).attr("data-uId", detailsData.data.title.uid)
$(".address").text(`${detailsData.data.title.position}`)
$(".like-number").text(`${detailsData.data.title.like}`)
$(".forward-number").text(`${detailsData.data.title.forward}`)
tId = parseInt($(".title p").attr("data-tid"))

//显示内容
$.each(detailsData.data.content, function (i, val) {
    let p = '', img = ''
    if (val.content) {
        p = `<p>${val.content}</p>`
    }
    if (val.img) {
        img = `<img src="${val.img}">`
    }
    bodyPart += `<section class="content">${p}${img}</section>`
})
$(".body-part").html(bodyPart)

showComment(commentData.data)

//显示评论
function showComment(data) {
    if (data == 0) {
        $(".comment-content").html(`<section class="no-content">
        <section></section>
         <p>暂无内容</p>
         </section>`)
    }
    if (data.length) {
        let userComments = ''
        $.each(data, function (i, val) {
            let reply = ''
            $.each(val.reply, function (i, val) {
                if (val.headimg) {
                    reply += `
            <section class="user-child-reply">
                    <section class="child-reply-head"><img src="${val.headimg}" alt=""></section>
            <section class="child-reply">
                <section class="child-reply-name" data-uId="${val.uid}">${val.username}</section>
                <section class="child-reply-content">${val.ccontent}</section>
            </section>
            </section>
        `
                } else {
                    reply += `
            <section class="user-child-reply">
                    <section class="child-reply-head"><p>${val.name.substr(-2)}</p></section>
            <section class="child-reply">
                <section class="child-reply-name" data-uId="${val.uid}">${val.username}</section>
                <section class="child-reply-content">${val.ccontent}</section>
            </section>
            </section>
        `
                }
            })
            if (val.headimg) {
                userComments += `
            <section class="user-comments">
            <!-- 该评论用户的头像 -->
            <section class="adult-comments-head"><img src="${val.headimg}" alt=""></section>
            <section class="user-adult-comments">
            <section class="adult-comments">
            <section class="adult-comments-information">
            <section class="adult-comments-name" data-uId="${val.uid}">${val.username}</section>
            <section class="adult-comments-time">${val.time}</section>
            </section>
            <section class="adult-comments-content" data-cId="${val.id}">${val.ccontent}</section>
            </section>
            ${reply}
            </section>
        </section>`
            } else {
                userComments += `
            <section class="user-comments">
            <!-- 该评论用户的头像 -->
            <section class="adult-comments-head"><p>${val.name.substr(-2)}</p></section>
            <section class="user-adult-comments">
            <section class="adult-comments">
            <section class="adult-comments-information">
            <section class="adult-comments-name" data-uId="${val.uid}">${val.username}</section>
            <section class="adult-comments-time">${val.time}</section>
            </section>
            <section class="adult-comments-content" data-cId="${val.id}">${val.ccontent}</section>
            </section>
            ${reply}
            </section>
        </section>`
            }

        })
        userComments += `<section class="end">
        <section></section>
        <p>到底了</p>
        </section>`
        $(".comment-content").html(userComments)
    }
}

//点赞功能
$(".like-icon").on("click", function () {
    let isLikeNum = $(this).attr("data-isLike")
    if (isLikeNum == 1) {
        Sendajax("http://119.23.46.241:8080/login/cancelLikes", "post", {tid: tId}, token)
        $(this).css("background", "url('https://19jwshow.oss-cn-shenzhen.aliyuncs.com/images/greylike.png') no-repeat center center")
        $(this).siblings("p").text(parseInt($(this).siblings("p").text()) - 1)
        $(this).attr("data-islike", 0)
    } else if (isLikeNum == 0) {
        Sendajax("http://119.23.46.241:8080/login/likes", "post", {tid: tId}, token)
        $(this).css("background", "url('https://19jwshow.oss-cn-shenzhen.aliyuncs.com/images/Heart%403x.png') no-repeat center center")
        $(this).siblings("p").text(parseInt($(this).siblings("p").text()) + 1)
        $(this).attr("data-islike", 1)
    } else {
        stop()
        layer.confirm('用户尚未登录，请先登录', {
            btn: ['确定']
            , btn1: function (index, layero) {
                window.location.href = 'userLoginPage.html'
                move()
            },
            title: '提示'
        });
    }
})

//评论
$(".comment-box input").keydown(function (e) {
    if (e.keyCode == 13) {
        if ($(this).val().trim().length > 0) {
            let commentNum = Sendajax("http://119.23.46.241:8080/login/details/addcomment", "post", {
                id: tId,
                text: $(this).val()
            }, token)
            if (commentNum.code == 201) {
                $(this).val('')
                commentData = Sendajax("http://119.23.46.241:8080/home/details/selComment", "post", {id: parseInt(detailsId)}, token)
                showComment(commentData.data)
                $(".comment-box input").val('')
                $(".comment-box-button").css("display", "none")
            }
        }
    }
})
$(".comment-box-button").on("click", function () {
    if ($(".comment-box input").val().trim().length > 0) {
        let commentNum = Sendajax("http://119.23.46.241:8080/login/details/addcomment", "post", {
            id: tId,
            text: $(".comment-box input").val()
        }, token)
        if (commentNum.code == 201) {
            $(this).val('')
            commentData = Sendajax("http://119.23.46.241:8080/home/details/selComment", "post", {id: parseInt(detailsId)}, token)
            showComment(commentData.data)
            $(".comment-box input").val('')
            $(".comment-box-button").css("display", "none")
        }
    }
})

$(".comment-box input").on("input", function () {
    if ($(this).val().trim().length != 0) {
        $(".comment-box-button").css("display", "block")
    } else {
        $(".comment-box-button").css("display", "none")
    }
})

//回复评论
$(".comment-content").on("click", ".adult-comments-content", function () {
    cId = parseInt($(this).attr("data-cid"))
    $(".comment-box").hide().siblings(".comment-header").toggleClass("fouce")
    $("#footer").after(`<section class="reply-comment">
        <section class="reply-comment-box">
        <input type="text" class="reply-comment-input">
        <button>发送</button>
        </section>
        </section>`)

    $(".reply-comment-input").focus()
    $(".reply-comment-input").keydown(function (e) {
        if (e.keyCode == 13) {
            replyComment(cId, $(this).val())
        }
    })

    $(".reply-comment-box button").on("click", function (e) {
        e.stopPropagation()
        replyComment(cId, $(this).siblings().val())
    })
    $(".reply-comment").on("click", function () {
        $(".comment-box").show().siblings(".comment-header").toggleClass("fouce")
        $(".reply-comment-box").remove()
        $(this).remove()
    })
})

function replyComment(cid, text) {
    if (text.trim().length > 0) {
        let replyCommentNum = Sendajax("http://119.23.46.241:8080/login/details/replycomment", "post", {
            id: tId,
            cid: cid,
            text: text
        }, token)
        if (replyCommentNum.code == 201) {
            $(".comment-box").show().siblings(".comment-header").toggleClass("fouce")
            commentData = Sendajax("http://119.23.46.241:8080/home/details/selComment", "post", {id: parseInt(detailsId)}, token)
            showComment(commentData.data)
            $(".reply-comment").remove()
        }
    }
}


//分享功能
$(".forward").on("click", function () {
    let id = parseInt($(".title p").attr("data-tid")),
        shareNum = $(this).find(".forward-number").text(),
        shareData = Sendajax("http://119.23.46.241:8080/home/share", "post", {id: id}, token)
    showShare(shareData.data)
    $(this).find(".forward-number").text(parseInt(shareNum) + 1)
    $(".share-box").css("display", "block").on("click", function () {
        $(this).css("display", "none")
        move()
    })
    stop()
})


function showShare(data) {
    if (data.titImg) {
        $(".share-box").html(`    <section class="share-box-images">
        <section class="share-box-images-header">
            <p>截图后发给想要分享的小伙伴吧</p>
            <p>识别图中二维码就可以观看啦</p>
        </section>
        <section class="share-box-images-content-image">
            <img src="${data.titImg}"  alt="">
        </section>
        <section class="share-box-images-content-text">
            <p>${data.title}</p>
        </section>
        <section class="share-box-images-information">
            <section class="share-box-images-username">${data.username}</section>
            <section>
                <section class="share-box-images-time">${data.time}</section>
                <section class="share-box-images-address">${data.position}</section>
            </section>
        </section>
        <section class="share-box-images-QRcode"><img src="${data.qrcodeUrl}"></section>
    </section>`)
    } else {
        $(".share-box").html(`<section class="share-box-text">
            <section class="share-box-text-header">
                <p>截图后发给想要分享的小伙伴吧</p>
                <p>识别图中二维码就可以观看啦</p>
            </section>
            <section class="share-box-text-content">
                <p>${data.title}</p>
            </section>
            <section class="share-box-text-information">
                <section class="share-box-text-username">${data.username}</section>
                <section>
                    <section class="share-box-text-time">${data.time}</section>
                    <section class="share-box-text-address">${data.position}</section>
                </section>
            </section>
            <section class="share-box-text-QRcode"><img src="${data.qrcodeUrl}"></section>
        </section>`)
    }
}




